from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QMessageBox, QFileDialog

from libretranslatepy import LibreTranslateAPI

import sys

import pytesseract
import cv2


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1002, 351)
        MainWindow.setStyleSheet("background-color: rgb(39, 39, 39);\n"
"color: rgb(255, 255, 255);\n"
"\n"
"\n"
"QTextEdit {\n"
"    background-color: rgb(71, 71, 71);\n"
"    padding:15px 15px;\n"
"}\n"
"\n"
"")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.comboBox = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox.setGeometry(QtCore.QRect(0, 10, 87, 32))
        self.comboBox.setObjectName("comboBox")
        self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
        self.comboBox_2.setGeometry(QtCore.QRect(910, 10, 87, 32))
        self.comboBox_2.setObjectName("comboBox_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(410, 0, 191, 51))
        self.label.setStyleSheet("font: 81 20pt \"Noto Sans\";")
        self.label.setObjectName("label")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 50, 1001, 301))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.textEditEng = QtWidgets.QTextEdit(self.gridLayoutWidget)
        self.textEditEng.setObjectName("textEditEng")
        self.gridLayout.addWidget(self.textEditEng, 0, 2, 5, 1)
        self.Translate = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.Translate.setStyleSheet("background-color: rgb(46, 119, 255);\n"
"border-radius: 5px;")
        self.Translate.setObjectName("Translate")
        self.gridLayout.addWidget(self.Translate, 1, 1, 1, 1)
        self.Image = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.Image.setStyleSheet("background-color: rgb(46, 119, 255);\n"
"border-radius: 5px;")
        self.Image.setObjectName("Image")
        self.gridLayout.addWidget(self.Image, 2, 1, 1, 1)
        self.textEditRu = QtWidgets.QTextEdit(self.gridLayoutWidget)
        self.textEditRu.setObjectName("textEditRu")
        self.gridLayout.addWidget(self.textEditRu, 0, 0, 5, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")


        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.libretranslateapi = LibreTranslateAPI("https://translate.astian.org/")

        self.add_lang()
        self.MainButton()

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "              Libretranslate"))
        self.label.setText(_translate("MainWindow", "Libretranslate"))
        self.Translate.setText(_translate("MainWindow", "Translate"))
        self.Image.setText(_translate("MainWindow", "Image"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))

    def add_lang(self):

        lang = self.libretranslateapi.languages()

        for lang in lang:
            self.comboBox.addItem(lang.get('code'))
            self.comboBox_2.addItem(lang.get('code'))
            self.comboBox_2.setCurrentText("ru")

        self.comboBox.addItem('auto')

    def MainButton(self):
        self.Translate.clicked.connect(self.Translate_Text)
        self.Image.clicked.connect(self.image_translate)

    def Translate_Text(self):
        try:
            main = self.textEditRu.toPlainText()
            lang_1 = self.comboBox.currentText()
            lang_2 = self.comboBox_2.currentText()
            
            translate = self.libretranslateapi.translate(main, lang_1, lang_2)
      
            self.textEditEng.setText(translate)
        except:

            error = QMessageBox()
            error.setWindowTitle("Warning")
            error.setText("You didn't introduce the text")
            error.setIcon(QMessageBox.Warning)
            error.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
            error.exec_()

    def image_translate(self):
        try:
            fname = QFileDialog.getOpenFileName(MainWindow)[0]
            img = cv2.imread(str(fname))
            text = pytesseract.image_to_string(img)
            self.textEditRu.setText(text)
        except:
            pass

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())